package ua.kiev.tosik.price.conf;

import org.apache.log4j.Logger;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SkinInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SkinsConfig {

	private static SkinsConfig ourInstance;
	private static Logger logger = Logger.getLogger(SkinsConfig.class);
	private List<Skin> skinList = new ArrayList<Skin>();
	private Skin defaultSkin;
	private String message;

	public static SkinsConfig getInstance() {
		if ( ourInstance == null ) {
			ourInstance = new SkinsConfig();
		}
		return ourInstance;
	}

	private SkinsConfig() {
		logger.debug("Initialize skins");
		Map<String, SkinInfo> sl = SubstanceLookAndFeel.getAllSkins();
		for (String key : sl.keySet()) {
			SkinInfo si = sl.get(key);
			Skin skin = new Skin();
			skin.setClassName(si.getClassName());
			skin.setName(si.getDisplayName());
			skinList.add(skin);
		}
		logger.debug("Found " + skinList.size() + " skins");
		detectDefaultSkin();
	}

	public Skin getDefaultSkin() {
		return defaultSkin;
	}

	public void setDefaultSkin(Skin defaultSkin) {
		this.defaultSkin = defaultSkin;
		try {
			AppConfig.getInstance().set("app.skin", defaultSkin.getName());
		} catch (Exception e ) {
			logger.warn(e.getLocalizedMessage());
		}
	}

	public List<Skin> getSkinList() {
		return skinList;
	}

	private void detectDefaultSkin() {
		Skin skin = null;
		try {
			if ( !AppConfig.getInstance().get("app.skin").equals("") ) {
				for ( Skin s: skinList ) {
					if ( s.getName().equals(AppConfig.getInstance().get("app.skin")) ) {
						skin = s;
						skin.setSelected(true);
						break;
					}
				}
			}
		} catch (Exception e) {
			logger.warn(e.getLocalizedMessage());
		}
		defaultSkin = (skin == null)?skinList.get(0):skin;
		logger.debug("Default skin is: " + defaultSkin.getName());
	}

}
